package com.czjtxy.extraTerm.serverImpl;

import com.czjtxy.extraTerm.dao.TdeptDao;
import com.czjtxy.extraTerm.daoImpl.TdeptDaoImpl;
import com.czjtxy.extraTerm.entity.DO.Tdept;
import com.czjtxy.extraTerm.server.DeptService;
import com.czjtxy.extraTerm.util.Util;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhangYing
 * @class 软件1903
 * @studentID 19852035
 * @date 2021/7/1
 */
public class DeptServiceImpl implements DeptService {
    private TdeptDao tdeptDao = new TdeptDaoImpl();

    @Override
    public List<Tdept> getall() throws SQLException {
        List<Tdept> tdepts = new ArrayList<>();
        try {
            tdepts = tdeptDao.getall();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tdeptDao.closeResource();
        }
        return tdepts;
    }

    @Override
    public Tdept getOne(int id) throws SQLException {
        Tdept tdept = null;
        try {
            tdept = tdeptDao.getOne(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tdeptDao.closeResource();
        }
        return tdept;
    }

    @Override
    public void updateDept(Tdept tdept) throws SQLException {
        try {
            tdeptDao.updateDept(tdept);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tdeptDao.closeResource();
        }
    }

    @Override
    public void deleteDept(String ids) throws SQLException, ClassNotFoundException {
        String[] is = ids.split(",");
        try {
            for (String s : is) {
                tdeptDao.deleteDept(Util.stringToInt(s));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tdeptDao.closeResource();
        }
    }

    @Override
    public void addDept(Tdept tdept) throws SQLException {
        try {
            tdeptDao.addDept(tdept);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tdeptDao.closeResource();
        }
    }

    @Override
    public List<Tdept> getPart(String name) throws SQLException {
        List<Tdept> tdepts = new ArrayList<>();
        TdeptDao tdeptDao = new TdeptDaoImpl();
        try {
            tdepts = tdeptDao.getPart(name);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tdeptDao.closeResource();
        }
        return tdepts;
    }
}
